<template>
  <van-dialog
    v-model:show="showDialog"
    title="物料调增调减"
    show-cancel-button
    @confirm="onConfirm"
  >
    <van-form>
      <van-field
        v-model="formData.productNo"
        name="productNo"
        label="产品"
        :rules="[{ required: true, message: '请选择产品' }]"
        readonly
        clickable
      />
      
      <van-field
        v-for="(item, index) in formData.updateList"
        :key="index"
        v-model="item.warehouseNumber"
        :name="`warehouseNumber-${index}`"
        :label="`仓库${index + 1}`"
        :rules="[{ required: true, message: '请输入仓库编号' }]"
        readonly
      />
      
      <van-field
        v-for="(item, index) in formData.updateList"
        :key="index"
        v-model="item.newQuantity"
        :name="`newQuantity-${index}`"
        :label="`数量${index + 1}`"
        :rules="[{ required: true, message: '请输入调整数量' }]"
        type="number"
      />
      
      <!-- <van-button type="primary" @click="addWarehouse">添加仓库</van-button> -->
      
      <van-field
        v-model="formData.reason"
        name="reason"
        label="原因"
        :rules="[{ required: true, message: '请输入调增调减原因' }]"
      />
    </van-form>
    
    <van-popup v-model:show="showProductPicker" position="bottom">
      <SelectProduct @select="onSelectProduct" />
    </van-popup>
  </van-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { showToast } from 'vant'
import { adjustProductQuantity, queryProductMessage } from '@/api/Base/product'
import SelectProduct from './SelectProduct.vue'

const props = defineProps({
  productNo: {
    type: String,
    required: true
  }
})

const showDialog = ref(false)
const showProductPicker = ref(false)
const warehouseList = ref([])

const formData = ref({
  productNo: props.productNo,
  updateList: [],
  reason: ''
})

// 获取库存信息
const getProductQuantity = async () => {
  try {
    const res = await queryProductMessage({
      productNumber: props.productNo
    })
    if (res.data?.quantityMessage) {
      warehouseList.value = res.data.quantityMessage
      formData.value.updateList = res.data.quantityMessage.map(item => ({
        warehouseNumber: item.sWarehouseNumber,
        newQuantity: item.nQuantity,
        productQuantityId: item.nProductQuantityId
      }))
    }
  } catch (error) {
    showToast('获取库存信息失败')
  }
}

watch(() => props.productNo, () => {
  formData.value.productNo = props.productNo
  getProductQuantity()
}, { immediate: true })

const open = () => {
  showDialog.value = true
}

const onSelectProduct = (product) => {
  formData.value.productNo = product.productNo
  showProductPicker.value = false
}

const addWarehouse = () => {
  formData.value.updateList.push({
    warehouseNumber: '',
    newQuantity: 0,
    productQuantityId: Date.now()
  })
}

const onConfirm = async () => {
  try {
    await adjustProductQuantity(formData.value)
    showToast('操作成功')
    showDialog.value = false
  } catch (error) {
    showToast('操作失败')
  }
}

defineExpose({
  open
})
</script>
